<?php

namespace app\service;
use think\Request;
use think\facade\Filesystem;
use think\facade\Log;
use app\controller\BaseController;
use app\model\Packages;
use app\model\PackageVersions;

class PublishService extends BaseController{
    private $userId;

    public function __construct() {
        $this->userId=1;

    }

    public function checkVersionNumber(Request $input){
        $packageName=$input->param('packageName','');
        $version=$input->param('version','');

        $packageResult=Packages::getUsersPackagesInfo($this->userId,$packageName);
        $packageVersionsResult=PackageVersions::getPackagesVersionsInfo($packageResult->id,$version);
        return $this->return_succ([
            "data"=>[
                "packageName"=>$packageName,
                "version"=>$packageVersionsResult->version,
            ]
        ]);
    }

    //上传包
    public function updatePackage($input){

        $packageName=$input->param('packageName','');
        $metadata=$input->param('metadata','');

        if(empty($packageName)){
            return $this->return_err(['msg'=>'缺少参数packageName']);
        }
        if(empty($metadata)){
            return $this->return_err(['msg'=>'缺少参数metadata']);
        }

        // 获取表单上传文件
        $file = $input->file('package');

        // 验证文件是否上传成功
        if (empty($file)) {
            return $this->return_err(['msg'=>'文件上传失败']);
        }
        $fileSize = $file->getSize();
        $maxSize = 10 * 1024 * 1024; // 1MB，设置最大允许文件大小
        if ($fileSize > $maxSize) {
            return $this->return_err(['msg'=>'文件大小超过限制，最大允许文件大小为2MB']);
        }

        //后缀名
        $fileExtension = $file->getOriginalExtension();
        if ($fileExtension!='gz') {
            return $this->return_err(['msg'=>'文件类型错误'.$fileExtension]);
        }
        /*
        $directory=$packageName.'/';
        //判断文件夹是否存在
        if (!file_exists($directory)) {
            // 创建文件夹
            if (mkdir($directory)) {
                // echo "文件夹创建成功";
            }
        }*/

        //原始名称
        $originalFileName = $file->getOriginalName();
        // 将文件移动到指定的存储路径，这里使用本地存储示例，存储在public/packages目录下
        $info = Filesystem::disk('public')->putFileAs('packages/'.$packageName.'/', $file,$originalFileName);
        if (!$info) {
            return $this->return_error();
        }

        $metadataArray = json_decode($metadata, true);
        $packageId=0;
        $result=Packages::getUsersPackagesInfo($this->userId,$packageName);
        // Log::record($result);
        if(!$result){
            $packageId=Packages::insertPackagesData([
                "user_id"=>$this->userId,
                "name"=>$packageName,
                "description"=>$metadataArray["description"],
                "homepage_url"=>"http://local.nest.cn/".$packageName,
                "repository_url"=>"http://local.nest.cn/".$packageName,
                "keywords"=>$packageName,
                "license"=>$metadataArray["license"],
                "author"=>$metadataArray["author"],
                "create_time"=>date("Y-m-d H:i:s"),
                "update_time"=>date("Y-m-d H:i:s"),
            ]);

        }else{
            $packageId=$result->id;
            $result=Packages::updatePackagesData($this->userId,$packageName,[
                "description"=>$metadataArray["description"],
                "homepage_url"=>"http://local.nest.cn/".$packageName,
                "repository_url"=>"http://local.nest.cn/".$packageName,
                "keywords"=>$packageName,
                "license"=>$metadataArray["license"],
                "author"=>$metadataArray["author"],
                "update_time"=>date("Y-m-d H:i:s"),
            ]);
        }
        PackageVersions::insertPackageVersionsData([
            "package_id"=>$packageId,
            "version"=>$metadataArray["version"],
            "description"=>$metadataArray["description"],
            "metadata"=>$metadata,
            "checksum"=>$metadataArray["checksum"],
            "size"=>$fileSize,
            "download_url"=>"http://local.nest.cn/packages/".$packageName."/".$originalFileName,
            "release_notes"=>"",
            "create_time"=>date("Y-m-d H:i:s"),
            "update_time"=>date("Y-m-d H:i:s"),
        ]);

        return $this->return_succ([
            "packageName"=>$packageName,
        ]);
    }
}
